package com.nkl.page.dao;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import com.nkl.common.dao.BaseDao;
//import com.nkl.common.util.DateUtil;
import com.nkl.common.util.StringUtil;
import com.nkl.page.domain.Note;

public class NoteDao {

	public int addNote(Note note, Connection conn){
		String sql = "INSERT INTO note(note_id,note_title,note_content,note_picture,note_time,note_admin) values(null,?,?,?,?,?)";
		Object[] params = new Object[] {
			note.getNote_title(),
			note.getNote_content(),
			note.getNote_picture(),
			note.getNote_time(),
			note.getNote_admin()

		};
		return BaseDao.executeUpdate(sql, params, conn );
	}

	public int delNote(String note_id, Connection conn){
		String sql = "DELETE FROM note WHERE note_id=?";

		Object[] params = new Object[] { new Integer(note_id)};
		return BaseDao.executeUpdate(sql, params, conn);
	}

	public int delNotes(String[] note_ids, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		for (int i = 0; i <note_ids.length; i++) {
			sBuilder.append("?");
			if (i !=note_ids.length-1) {
				sBuilder.append(",");
			}
		}
		String sql = "DELETE FROM note WHERE note_id IN(" +sBuilder.toString()+")";

		Object[] params = note_ids;

		return BaseDao.executeUpdate(sql, params, conn);	}

	public int updateNote(Note note, Connection conn){
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("UPDATE note SET note_id = " + note.getNote_id());
		if (!StringUtil.isEmptyString(note.getNote_title())) {
			sBuilder.append(",note_title = '" + note.getNote_title() + "'");
		}
		if (!StringUtil.isEmptyString(note.getNote_content())) {
			sBuilder.append(",note_content= '" + note.getNote_content() + "'");
		}
		if (!StringUtil.isEmptyString(note.getNote_admin())) {
			sBuilder.append(",note_admin= '" + note.getNote_admin() + "'");
		}

		sBuilder.append(" where note_id = " + note.getNote_id());

		Object[] params = null;
		return BaseDao.executeUpdate(sBuilder.toString(), params, conn);	}

	public Note getNote(Note note, Connection conn){
		Note _note=null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM note WHERE note_id = " + note.getNote_id());

		List<Object> list = BaseDao.executeQuery(Note.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			 _note = (Note)list.get(0);
		}
		return _note;
	}

	public List<Note>  listNotes(Note note, Connection conn){
		List<Note> notes = null;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT * FROM (");
		sBuilder.append("SELECT * FROM note WHERE 1=1");
		
		if (note.getNote_id() !=0) {
			sBuilder.append(" and note_id = " + note.getNote_id());
		}
		if (!StringUtil.isEmptyString(note.getNote_title())) {
			sBuilder.append(" and note_title like '%" + note.getNote_title() + "%'");
		}
		sBuilder.append(" order by note_time desc,note_id asc) t");
		
		if (note.getStart() != -1) {
			sBuilder.append(" limit " + note.getStart() + "," + note.getLimit());
		}

		List<Object> list = BaseDao.executeQuery(Note.class.getName(), sBuilder.toString(), null, conn);
		if (list != null && list.size() > 0) {
			notes = new ArrayList<Note>();
			for (Object object : list) {
				notes.add((Note)object);
			}
		}
		return notes;
	}
	
	public int listNotesCount(Note note, Connection conn){
		int sum = 0;
		StringBuilder sBuilder = new StringBuilder();
		sBuilder.append("SELECT count(*) FROM note WHERE 1=1");
		
		if (note.getNote_id() !=0) {
			sBuilder.append(" and note_id = " + note.getNote_id());
		}
		if (!StringUtil.isEmptyString(note.getNote_title())) {
			sBuilder.append(" and note_title like '%" + note.getNote_title() + "%'");
		}
		
		long count = (Long)BaseDao.executeQueryObject(sBuilder.toString(), null, conn);
		sum = (int)count;
		
		return sum;	
	}

}
